package spark.core.scala

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkContext, SparkConf}

/**
 * Created by Administrator on 2018/2/10/010.
 */
object Top3 {


  def main(args: Array[String]) {
    //1.spark上下文
    val conf = new SparkConf()
      .setMaster("local[*]")
      .setAppName("Top3")
    val sc = new SparkContext(conf)
    //2.读取数据块，生成RDD（集合、数据集）
    val url = ("datas/top.txt")
    val lines :RDD[String] = sc.textFile(url)
    val pairs = lines.map(line => (line.toInt,line))
    val sortedPairs = pairs.sortByKey(false)
    val sortedNumbers = sortedPairs.map(sortPair => sortPair._1)
    val top3Numbers = sortedNumbers.take(3)
    for(num <- top3Numbers){
      println(num)
    }
  }
}
